<?php
namespace app\modules\v1\controllers;

use app\modules\common\components\Api;
use app\modules\common\components\Common;
use app\modules\common\components\YiiLog;
use app\modules\common\filters\LoginFilter;

use app\modules\v1\services\business\LoginBusiness;
use app\modules\v1\services\validate\LoginValidate;
use Yii;
use yii\web\Controller;

class LoginController extends Controller
{
    public function behaviors()
    {
        return [
            'login'=>[
                'class' => LoginFilter::className(),
                'only' => ['info'],
                // 'except' => ['count']
            ]
        ];
    }

    public function actionInfo()
    {
        $request = Yii::$app->request;
        $initData = $request->post();
        $userInfo = $request->getQueryParam("userInfo");
        $initData['userId'] = $userInfo['userId'] ?? "";

        $validateResult = (new LoginValidate())->doInfo($initData);
        if ($validateResult['code'] != Api::CODE_SUCCESS) {
            return Common::jsonReturn($validateResult['code'], $validateResult['desc'], $validateResult['data']);
        }

        //业务处理
        $doResult = (new LoginBusiness())->doInfo($initData);
        return Common::jsonReturn($doResult['code'], $doResult['desc'], $doResult['data']);
    }


    public function actionDolog()
    {
        $request = Yii::$app->request;
        $initData = $request->post();
        
        YiiLog::formatInfo(json_encode($initData));
        //参数检验
        $validateResult = (new LoginValidate())->doLog($initData);
        if ($validateResult['code'] != Api::CODE_SUCCESS) {
            return Common::jsonReturn($validateResult['code'], $validateResult['desc'], $validateResult['data']);
        }
        //业务处理
        $doResult = (new LoginBusiness())->doLog($initData);
        return Common::jsonReturn($doResult['code'], $doResult['desc'], $doResult['data']);
    }
}
